package com.example.demo.controller;

import com.example.demo.data.ResultDate;
import com.example.demo.pojo.Menu;
import com.example.demo.pojo.Role;
import com.example.demo.pojo.RoleMenu;
import com.example.demo.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("role")
@Transactional  //开启事务回滚
public class RoleController {

    @Autowired
    private RoleService roleService;

    //多条件分页查询
    @GetMapping("pageInfoByName")
    public ResultDate<Role> getPageInfoByName(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                              @RequestParam(value = "pageSize",defaultValue = "5") int pageSize,
                                              @RequestParam("roleName") String roleName){

        PageInfo<Role> rolePageInfo = roleService.findByName(pageNum, pageSize,roleName);

        if (rolePageInfo != null){

            ResultDate<Role> resultDate = new ResultDate<>();
            resultDate.setPageInfo(rolePageInfo);
            return resultDate;

        }

        return null;
    }

    //获取所有menu列表
    @GetMapping("menuList")
    public ResultDate<Menu> getAllMenu(){

        List<Menu> allMenu = roleService.getAllMenu();
        ResultDate<Menu> resultDate = new ResultDate<>();
        if (allMenu != null){
            resultDate.setList(allMenu);

        }else {
            resultDate.setStatus(-1);
            resultDate.setMsg("服务端内部出错！");
        }

        return resultDate;

    }

    //添加角色
    @PostMapping("add")
    public ResultDate<Object> addRole(@RequestParam("roleName") String roleName,
                                      @RequestParam("roleStatus") int roleStatus,
                                      @RequestParam("ids") String ids){//此处需要对前端的ids进行处理

        //处理ids数据;字符串格式  "["1","6","10","12"]"
        String[] s = ids.replaceAll("\\[|\\]", "").split(",");
        //正则不熟，处理字符串痛苦呀
        List<Integer> mids = new ArrayList<>();
        for(String s1 : s) {
            //取出“”包含的数字
            String a = s1.replaceAll("\\W", "");
            mids.add(Integer.valueOf(a));

        }

        //涉及到多表操作
        Role role = new Role();
        role.setR_name(roleName);
        role.setR_state(roleStatus);

        //添加角色信息到role表
        int i = roleService.addRole(role);

        //泛型为Object,因为要存多种对象数据
        ResultDate<Object> resultDate = new ResultDate<>();

        //判断新增角色是否成功
        if (i >=1 ){
            Integer r_id = role.getR_id();

            //用来存放role_menu表的多个数据
            List<Object> roleMenus = new ArrayList<>();

            for(int m_id : mids) {

                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setR_id(r_id);
                roleMenu.setM_id(m_id);

                //添加中间表Role_Menu数据
                int flag = roleService.addRole_Menu(roleMenu);
                //判断role_menu表数据是否新增成功
                if (flag >= 1){
                    //循环存数据
                    roleMenus.add(roleMenu);
                }else {
                    roleMenus.add("第"+m_id+"号资源未添加成功！");
                }

            }

            resultDate.setStatus(1);
            resultDate.setList(roleMenus);
            resultDate.setObj(role);

        }else {
            resultDate.setStatus(-1);
            resultDate.setMsg("服务端内部错误！");
        }

        return resultDate;
    }

    //添加页面的唯一性验证
    @GetMapping("compare")
    public ResultDate<Role> compare(@RequestParam("roleName") String roleName){

        Role role = new Role();
        role.setR_name(roleName);

        List<Role> compare = roleService.compare(role);

        ResultDate<Role> resultDate = new ResultDate<>();
        //集合元素大于等于1说明数据库中含值，不能使用
        if (compare.size() >=1 ){
            resultDate.setStatus(-1);
            resultDate.setMsg("角色名冲突，请重新输入角色名");
            resultDate.setList(compare);
        }else {
            resultDate.setStatus(1);
            resultDate.setMsg("角色名可以使用~");
            resultDate.setList(compare);
        }

        return resultDate;
    }

    //更新前获取数据库原信息展示到页面
    @PostMapping("getInfoById")
    public ResultDate<Object> getInfoById(@RequestParam("id") int roleId){
        //获取角色信息
        Role role = roleService.getInfoById(roleId);

        //获取中间表的m_id信息
        List<RoleMenu> roleMenu = roleService.getRoleMenuByRoleId(roleId);
        List<Object> m_ids = new ArrayList<>();
        for(RoleMenu menu : roleMenu) {
            Integer m_id = menu.getM_id();

            m_ids.add(m_id);
        }

        ResultDate<Object> resultDate = new ResultDate<>();
        //角色资源信息
        resultDate.setList(m_ids);
        //角色基本信息
        resultDate.setObj(role);

        return resultDate;
    }

    //更新角色信息
    @PostMapping("update")
    public ResultDate<Object> updateRole(@RequestParam("roleId") int roleId,
                                         @RequestParam("roleName") String roleName,
                                         @RequestParam("roleStatus") int roleStatus,
                                         @RequestParam("ids") String ids){

        //处理ids数据;字符串格式  "["1","6","10","12"]"
        String[] s = ids.replaceAll("\\[|\\]", "").split(",");
        //正则不熟，处理字符串痛苦呀
        List<Integer> mids = new ArrayList<>();
        for(String s1 : s) {
            //取出“”包含的数字
            String a = s1.replaceAll("\\W", "");
            mids.add(Integer.valueOf(a));

        }

        //涉及到多表操作
        Role role = new Role();
        role.setR_id(roleId);
        role.setR_name(roleName);
        role.setR_state(roleStatus);
        //更新role表信息
        int flag = roleService.updateRole(role);

        ResultDate<Object> resultDate = new ResultDate<>();
        if (flag >= 1){
            //更新中间表数据
            //中间表数据先根据roleId删除原有数据，然后再重新插入新数据
            //存放中间表数据
            List<Object> roleMenus = new ArrayList<>();

            //删除原有数据
            int flag1 = roleService.delRole_Menu_By_RoleId(roleId);

            if (flag1 >= 1){
                //重新添加中间表role_menu数据
                for(Integer mid : mids) {

                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setR_id(roleId);
                    roleMenu.setM_id(mid);

                    //新增数据
                    int flag2 = roleService.addRole_Menu(roleMenu);

                    if (flag2 >= 1){
                        roleMenus.add(roleMenu);
                    }else {
                        roleMenus.add("第"+mid+"号资源未更新成功！");
                    }
                }
            }

            resultDate.setStatus(1);
            resultDate.setList(roleMenus);
            resultDate.setObj(role);
        }else {
            resultDate.setStatus(-1);
            resultDate.setMsg("服务端内部错误！");
        }

        return  resultDate;
    }

    //通过Role_ID删除角色信息，以及角色中间表数据
    @PostMapping("delete")
    public ResultDate<Object> deleteInfo(@RequestParam("id") int roleId){

        ResultDate<Object> resultDate = new ResultDate<>();
        //删除角色信息
        int flag = roleService.deleteRole(roleId);
        if (flag >=1){
            //删除中间表数据
            int flag1 = roleService.delRole_Menu_By_RoleId(roleId);
            if (flag1 >=1 ){
                resultDate.setMsg("数据删除成功~");
            }else {
                //故意产生异常，使数据可以进行回滚，保持事务的一致性
                try {
                    int b = 10/0;
                }catch (Exception e){
                    resultDate.setStatus(-1);
                    resultDate.setMsg("中间表数据删除失败！");
                    e.printStackTrace();
                }

            }
        }else {
            //故意产生异常，使数据可以进行回滚，保持事务的一致性
            try{
                int a = 10/0;
            }catch (Exception e){
                resultDate.setStatus(-1);
                resultDate.setMsg("角色信息删除失败！");
                e.printStackTrace();
            }

        }

        return resultDate;
    }

}

